package ext.vpde.cadvaulting.migration;

import java.io.*;
import java.util.*;

import ext.vpde.cadvaulting.util.WINDSQL;

public class UpdateAttributes {

	public static void main ( String [] args ) throws Exception {
		if (args.length == 1) readEntries(args[0]);
		else System.out.println("\nUsage: java.ext.vpde.cadvaulting.UpdateAttributes loadfile");
    }
	
	private static void readEntries(String inputFile) throws Exception {
		System.out.println("BEGIN UPDATE ATTRIBUTES: " + Calendar.getInstance().getTime());
		WINDSQL.openConnection();
		BufferedReader file = LoadUtil.openInputFile(inputFile);
		if (file != null) {
			String s = file.readLine();
			int lineNumber = 0;
			while (s!= null && s.length() > 0) {
				LoadUtil.printProgress(++lineNumber);
				Hashtable ht = LoadUtil.processInputLine(s);
				String CADName = ht.get("CADNAME").toString();
				String docVer  = ht.get("VERSION").toString();
				String docIter = ht.get("ITERATION").toString();
		    	String source    = ht.get("SOURCE").toString();
		    	String sourceId  = ht.get("SOURCEID").toString();
		    	String creator   = ht.get("CREATOR").toString();
		    	String created   = ht.get("CREATED").toString();
		    	String modifier  = ht.get("MODIFIER").toString();
		    	String modified  = ht.get("MODIFIED").toString();
				if (WINDSQL.documentExists(CADName,docVer,docIter)) {
					try {
				    	String ida2a2 = WINDSQL.getDocument(CADName,docVer,docIter);
				    	WINDSQL.createSourceIBA(ida2a2, source, sourceId);
			   			WINDSQL.fixCreateStamp(ida2a2,creator,created,modifier,modified);
				    } catch (Exception e) { LoadUtil.printException("UpdateAttributes.readEntries", e); }
				}
				s = file.readLine();
			}
		}
		WINDSQL.closeConnection();
		System.out.println("-END- UPDATE ATTRIBUTES: " + Calendar.getInstance().getTime());
	}
}